package com.eye.catcher.rabbit;

import com.alibaba.fastjson.JSONObject;
import com.eye.catcher.constants.RedisConstants;
import com.eye.catcher.constants.SbCode;
import com.eye.catcher.system.user.param.UserDto;
import com.eye.catcher.system.user.param.UserLoginInfo;
import com.eye.catcher.common.RedisUtils;
import com.eye.catcher.log.param.LogDto;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Component
public class RabbitMessageUtils {

    @Autowired
    private AmqpTemplate amqpTemplate;
    @Autowired
    private RedisUtils redisUtils;

    /**
     * @author huzhiwei
     * @since 2022-02-08
     * @param obj
     * @param s
     * @param s1
     */
    public void sendAmqp(Object obj, String s, String s1){

        String messageId = String.valueOf(UUID.randomUUID());
        String messageData = "test message, hello!";
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
        map.put("messageData",obj);
        map.put("createTime",createTime);
        //将消息携带绑定键值：OperationDirectRouting OperationDirectExchange
        amqpTemplate.convertAndSend(s, s1, map);
    }

    /**
     * <p>
     *  发送消息给mq
     * </p>
     * @author huzhiwei
     * @since 2022-02-08
     * @param methodCode
     * @param methodName
     * @param moduleCode
     * @param moduleName
     * @param className
     * @param userId
     * @param sbCode
     * @param param
     * @param remark
     * @param runStage
     * @param returnText
     */
    public void sendRabbitMqLog(String methodCode, String methodName,
                                 String moduleCode, String moduleName,
                                 String className, String userId, SbCode sbCode,
                                 String param,String remark,String runStage,Object returnText,
                                JSONObject userAgent){
        UserLoginInfo userLoginInfo = null;
        try {
            String key = RedisConstants.USER_INFO_EXPIRE.getKey() + userId;
            userLoginInfo = (UserLoginInfo) redisUtils.get(key);

        }catch (Exception e){
            e.printStackTrace();
        }
        sendOperationAmqp(LogDto.builder()
                .userId(userLoginInfo.getUserDto().getId())
                .loginName(userLoginInfo.getUserDto().getLoginName())
                .userName(userLoginInfo.getUserDto().getUserName())
//                .orgId(userLoginInfo.getUserDto().getOrgId())
//                .orgName(userLoginInfo.getUserDto().getOrgName())
                .moduleCode(moduleCode)
                .moduleName(moduleName)
//                .systemCode(SbCode.LANXUM_SYSTEM.getCode())
//                .systemName(SbCode.LANXUM_SYSTEM.getMessage())
                .methodCode(className+" "+methodCode)
                .methodName(methodName)
                .operationCode(sbCode.getCode())
                .operationName(sbCode.getMessage())
                .param(param)
                .remark(userAgent.toString())
                .time(LocalDateTime.now())
                .runState(runStage)
                .returnText(null != returnText ? String.valueOf(returnText):null)
                .build());
    }


    public void sendInitRabbitMqLog(String methodCode,String moduleCode, String moduleName,
                                    String className, String userId, SbCode sbCode,String param,String remark,
                                    JSONObject userAgent){
        this.sendRabbitMqLog(methodCode,null,moduleCode,moduleName,className,userId, sbCode,
                param,remark,"init",null,userAgent);
    }
    public void sendFinishRabbitMqLog(String methodCode,String moduleCode, String moduleName,
                  String className, String userId, SbCode sbCode,String param,String remark,Object returnText,
                  JSONObject userAgent){
        this.sendRabbitMqLog(methodCode,null,moduleCode,moduleName,className,userId, sbCode,
                param,remark,"finish",returnText,userAgent);
    }

    public void sendOperationAmqp(Object obj){
        //将消息携带绑定键值：OperationDirectRouting OperationDirectExchange
        this.sendAmqp(obj,"OperationDirectExchange", "OperationDirectRouting");
    }
    public void sendAbnormalAmqp(Object obj){
        this.sendAmqp(obj,"AbnormalDirectExchange", "AbnormalDirectRouting");
    }
    public void sendLoginAmqp(Object obj){
        this.sendAmqp(obj,"LoginDirectExchange", "LoginDirectRouting");
    }

    public void sendLoginRabbitMqLog(String methodCode, String moduleCode, String moduleName,
                             String className, String param, Object returnText,String userId,String loginName,
                             JSONObject userAgent) {
        UserLoginInfo userLoginInfo = null;
        if(StringUtils.isNotEmpty(userId)){
            String key = RedisConstants.USER_INFO_EXPIRE.getKey()+userId;
            userLoginInfo = (UserLoginInfo)redisUtils.get(key);
            if(null == userLoginInfo){
                userLoginInfo = new UserLoginInfo();
                userLoginInfo.setUserDto(new UserDto());
            }
        }
        sendLoginAmqp(LogDto.builder()
                .userId(userId)
                .loginName(loginName)
                .userName(userLoginInfo.getUserDto().getUserName())
//                .orgId(userLoginInfo.getUserDto().getOrgId())
//                .orgName(userLoginInfo.getUserDto().getOrgName())
                .moduleCode(moduleCode)
                .moduleName(moduleName)
//                .systemCode(SbCode.LANXUM_SYSTEM.getCode())
//                .systemName(SbCode.LANXUM_SYSTEM.getMessage())
                .methodCode(className+" "+methodCode)
//                .methodName(methodName)
                .operationCode(SbCode.OPERATION_LOGIN.getCode())
                .operationName(SbCode.OPERATION_LOGIN.getMessage())
                .param(param)
                .remark(userAgent.toString())
                .time(LocalDateTime.now())
//                .runState(runStage)
                .returnText(null != returnText ? String.valueOf(returnText):null)
                .build());

    }

    public void sendAbnormalRabbitMqLog(String methodCode, String moduleCode, String moduleName,
                            String className, String userId, SbCode sbCode,String errorInfo,
                            JSONObject userAgent) {

        String key = RedisConstants.USER_INFO_EXPIRE.getKey()+userId;
        UserLoginInfo userLoginInfo = (UserLoginInfo)redisUtils.get(key);

        sendAbnormalAmqp(LogDto.builder()
                .userId(userLoginInfo.getUserDto().getId())
                .loginName(userLoginInfo.getUserDto().getLoginName())
                .userName(userLoginInfo.getUserDto().getUserName())
//                .orgId(userLoginInfo.getUserDto().getOrgId())
//                .orgName(userLoginInfo.getUserDto().getOrgName())
                .moduleCode(moduleCode)
                .moduleName(moduleName)
//                .systemCode(SbCode.LANXUM_SYSTEM.getCode())
//                .systemName(SbCode.LANXUM_SYSTEM.getMessage())
                .methodCode(className+" "+methodCode)
//                .methodName(methodName)
                .operationCode(sbCode.getCode())
                .operationName(sbCode.getMessage())
//                .param(param)
                .remark(userAgent.toString())
                .time(LocalDateTime.now())
//                .runState(runStage)
                .returnText(errorInfo)
                .build());
    }
}
